package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.LoginUser;
import com.cskaoyan.bean.PageInfo;
import com.cskaoyan.bean.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @author stone
 * @date 2022/07/12 09:58
 */
@RestController
@RequestMapping("user")
public class UserController {

    //localhost:8080/user/register1?username=songge&password=niupi&age=40
    @RequestMapping("register1")
    /*public BaseRespVo register1(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String age = request.getParameter("age");*/
    public BaseRespVo register1(String username, String password, String age) {

        return BaseRespVo.ok(username + " -- " + password + " -- " + age);
    }


    //可以使用基本类型，也可以使用包装类；更建议使用包装类 → 程序更健壮
    //localhost:8080/user/register2?username=songge&password=niupi&age=40
    @RequestMapping("register2")
    public BaseRespVo register2(String username, String password, Integer age) {
    //public BaseRespVo register2(String username, String password, int age) {

        return BaseRespVo.ok(username + " -- " + password + " -- " + age);
    }

    //localhost:8080/user/register3?username=songge&password=niupi&age=30&birthday=2022/07/12
    @RequestMapping("register3")
    public BaseRespVo register3(String username, String password, Integer age,
                                Date birthday) {
        return BaseRespVo.ok(birthday);
    }

    //yyyy-MM-dd这样的日期字符串类型的值能否接收呢？可以接收，但是需要指定日期格式
    //localhost:8080/user/register4?username=songge&password=niupi&age=30&birthday=2022-06-21
    @RequestMapping("register4")
    public BaseRespVo register4(String username, String password, Integer age,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {
        return BaseRespVo.ok(birthday);
    }

    //localhost:8080/user/register5?username=songge&password=niupi&age=30
    // &hobbies=sing&hobbies=dance&hobbies=rap
    // &ids=1&ids=2&ids=3
    @RequestMapping("register5")
    public BaseRespVo register5(String username, String password, Integer age,
                                String[] hobbies,
                                Integer[] ids) {
        System.out.println(Arrays.asList(ids));
        return BaseRespVo.ok(hobbies);
    }

    //localhost:8080/user/register6?username=songge&password=niupi&age=30
    // &hobbies=sing&hobbies=dance&hobbies=rap
    // &ids=1&ids=2&ids=3
    // &birthday=2022-06-21
    @RequestMapping("register6")
    /*public BaseRespVo register6(String username, String password, Integer age,
                                String[] hobbies,
                                Integer[] ids,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {*/
    public BaseRespVo register6(User user) {

        return BaseRespVo.ok(user);
    }

    //localhost:8080/user/query?age=20&page=1&limit=10
    @RequestMapping("query")
    //public BaseRespVo query(Integer age, Integer page, Integer limit) {
    public BaseRespVo query(Integer age, PageInfo pageInfo) {

        return BaseRespVo.ok(null);
    }

    // json字符串：{"username":"songge","password":"niupi"}
    // localhost:8080/user/login    POST
    // Content-Type:application/json
    @RequestMapping("login")
    /*public BaseRespVo login(@RequestBody String json) {
        LoginUser loginUser = jsonUtil.transfer(json);*/
    public BaseRespVo login(@RequestBody LoginUser user){//jackson会将字符串转换为引用类型的对象
        return BaseRespVo.ok(user);
    }

    //和上面的login请求只有请求url由login改为了login2，其余部分完全一样
    @RequestMapping("login2")
    public BaseRespVo login2(@RequestBody Map map){//jackson会将字符串转换为map
        String username = (String) map.get("username");
        String password = (String) map.get("password");

        return BaseRespVo.ok(map);
    }

}
